package com.uzai.console.web.user;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.entity.WxpubUser;
import com.uzai.common.enums.AccDetailTypeEnum;
import com.uzai.common.enums.IdentityEnum;
import com.uzai.common.enums.UserBlackTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.TbUserInfo.TbUserInfoIdDto;
import com.uzai.console.dto.TbUserInfo.TbUserInfoQuery;
import com.uzai.console.dto.TbUserInfo.TbUserInfoUpdateDto;
import com.uzai.console.dto.profit.ladderratiogroup.LadderRatioGroupQuery;
import com.uzai.console.dto.profit.prirewardrule.PriRewardRuleQuery;
import com.uzai.console.dto.user.UserUpdateMarketingTagDto;
import com.uzai.console.dto.user.useraccdetail.UserAccDetailQuery;
import com.uzai.console.dto.user.usertype.UserTypeQuery;
import com.uzai.console.dto.user.wechatuser.WechatUserQuery;
import com.uzai.console.dto.user.wxpubuser.*;
import com.uzai.console.dto.user.wxworkuser.WxworkUserUpdateDto;
import com.uzai.console.dto.user.wxworkuser.WxworkUserUpdateMarketingRewardByQueryResultDto;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatInfoQuery;
import com.uzai.console.dto.wxpub.UpdateWechatUserDto;
import com.uzai.console.dto.wxpub.WechatDeviceUniqueIdDto;
import com.uzai.console.entity.*;
import com.uzai.console.entity.wxpubmanage.WxpubInfoEntity;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SmsType;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.*;
import com.uzai.console.service.merchant.MerchantService;
import com.uzai.console.service.user.WxpubUserService;
import com.uzai.console.service.user.WxpubUserSyncService;
import com.uzai.console.vo.base.SelectIdVo;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.tbuserinfo.TbUserInfoVo;
import com.uzai.console.vo.user.useraccdetail.UserAccDetailInfoVo;
import com.uzai.console.vo.user.wechatuser.WechatUserDeciceInfoVo;
import com.uzai.console.vo.user.wxpubuser.WxpubUserDeciceInfoVo;
import com.uzai.console.vo.user.wxpubuser.WxpubUserInfoVo;
import com.uzai.console.vo.user.wxpubuser.WxpubUserPageVo;
import com.uzai.console.vo.user.wxpubuser.WxpubUserPriRewardRulesVo;
import com.uzai.console.vo.user.wxpubuser.WxpubUserTotalVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cen
 * @date 2022-3-29 16:06:29
 */
@Api(value = "微信公众号用户管理", tags = "用户管理|微信公众号用户管理")
@ApiSort(98)
@RestController
public class WxpubUserContoller extends BaseController {

    @Autowired
    private WxpubUserService wxpubUserService;

    @Autowired
    private WxpubUserSyncService wxpubUserSyncService;

    @Autowired
    private WxpubUserMapper wxpubUserMapper;
    @Autowired
    private PriRewardRuleMapper priRewardRuleMapper;
    @Autowired
    private LadderRatioGroupMapper ladderRatioGroupMapper;
    @Autowired
    private UserTypeMapper userTypeMapper;
    @Autowired
    private UserTypeGroupMapper userTypeGroupMapper;
    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private ConfigPlanMapper configPlanMapper;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;


    @ApiOperation(value = "分页查询微信公众号用户列表", notes = "分页查询微信公众号用户列表", response = WxpubUserPageVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/wxpubUser/findWxpubUserList")
    public Object findWxpubUserList(@RequestBody WxpubUserQuery wxpubUserQuery) {
        return wxpubUserService.findUserList(wxpubUserQuery);
    }

    @ApiOperation(value = "查询微信公众号用户详情", notes = "查询微信公众号用户详情", response = WxpubUserInfoVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/wxpubUser/findWxpubUserInfo")
    public Object findWxpubUserInfo(@RequestBody WxpubUserIdDto wxpubUserIdDto) {
        return wxpubUserService.findUserInfo(wxpubUserIdDto);
    }

    @ApiOperation(value = "查询会员身份下拉列表", notes = "查询会员身份下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 3)
    @PostMapping("/wxpubUser/findIdentityListSelect")
    public Object findIdentityListSelect() {
        List<SelectKeyVo> selectKeyVoList = new ArrayList<>();
        IdentityEnum[] IdentityEnumArr = IdentityEnum.values();
        if (IdentityEnumArr != null && IdentityEnumArr.length > 0) {
            for (IdentityEnum identityEnum : IdentityEnumArr) {
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                //页面显示 身份
                selectKeyVo.setKey(String.valueOf(identityEnum.getId()));
                selectKeyVo.setValue(identityEnum.getDesc());
                selectKeyVoList.add(selectKeyVo);
            }
        }
        return selectKeyVoList;
    }

    @ApiOperation(value = "查询会员类型", notes = "查询会员类型", response = SelectIdVo.class)
    @ApiOperationSupport(order = 4)
    @PostMapping("/wxpubUser/findUserTypeList")
    public Object findUserTypeList(@RequestBody UserTypeQuery userTypeQuery) {
        List<SelectIdVo> selectIdVoList = new ArrayList<>();
        List<UserType> userTypeList = userTypeMapper.findByList(userTypeQuery);
        if (userTypeList != null && userTypeList.size() > 0) {
            for (UserType userType : userTypeList) {
                SelectIdVo selectIdVo = new SelectIdVo();
                selectIdVo.setKey(userType.getId());
                //页面显示 身份-类型名称
                //页面显示 身份-类型名称
                UserTypeGroup userTypeGroup = userTypeGroupMapper.selectById(userType.getGroupId(), userType.getMerId());
                if(userTypeGroup == null){
                    userTypeGroup = new UserTypeGroup();
                }
                selectIdVo.setValue(Tools.getStr(userTypeGroup.getName())  + "-" + userType.getName() + "（"  + IdentityEnum.getById(userType.getIdentity()).getDesc() + "）");
                //是否默认
                selectIdVo.setIsDefault(userType.getIsDefault());
                selectIdVoList.add(selectIdVo);
            }
        }
        return selectIdVoList;
    }


    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除所查询的会员数据", notes = "删除所查询的会员数据")
    @ApiOperationSupport(order = 5)
    @PostMapping("/wxpubUser/deleteUserSelected")
    public Object deleteUserSelected(@RequestBody WxpubUserBatchDeleteDto wxpubUserBatchDeleteDto) {
        wxpubUserService.deleteUserSelected(wxpubUserBatchDeleteDto);
        return true;
    }

//    @ApiOperation(value = "查询定投项目下拉选择框列表", notes = "查询定投项目下拉选择框列表", response = CpaSelectVo.class)
//    @ApiOperationSupport(order = 6)
//    @PostMapping("/wxpubUser/findCpcSelectList")
//    public Object findCpcSelectList() {
//        Long merId = getLoginInfo().getMerId();
//        //通过merID查询定投项目
//        List<CpaSelectVo> cpaSelectVoList = new ArrayList<>();
//        CpaSelectVo cpaSelectVo_1 = new CpaSelectVo();
//        cpaSelectVo_1.setId(1L);
//        cpaSelectVo_1.setName("定投项目一");
//        cpaSelectVoList.add(cpaSelectVo_1);
//        CpaSelectVo cpaSelectVo_2 = new CpaSelectVo();
//        cpaSelectVo_2.setId(2L);
//        cpaSelectVo_2.setName("定投项目二");
//        cpaSelectVoList.add(cpaSelectVo_2);
//        return cpaSelectVoList;
//    }

//    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.UPDATE)
//    @ApiOperation(value = "单个发送消息", notes = "单个发送消息")
//    @ApiOperationSupport(order = 8)
//    @PostMapping("/wxpubUser/sendMessageToOneUser")
//    public Object sendMessageToOneUser(@RequestBody WxpubUserSendMsgDto wxpubUserSendMsgDto) {
//        //单个用户发送消息，不需要添加标签和修改备注
//        wxpubUserService.d(wxpubUserSendMsgDto);
//        return true;
//    }

    @ApiOperation(value = "查询下级成员", notes = "查询下级成员", response = WxpubUserPageVo.class)
    @ApiOperationSupport(order = 6)
    @PostMapping("/wxpubUser/findSonUserList")
    public Object findSonUserList(@RequestBody FindSonWxpubUserListQuery findSonWxpubUserListQuery) {
        return wxpubUserService.findSonUserList(findSonWxpubUserListQuery);
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除下级成员", notes = "删除下级成员")
    @ApiOperationSupport(order = 7)
    @PostMapping("/wxpubUser/deleteSonWxpubUser")
    public Object deleteSonWxpubUser(@RequestBody WxpubUserDeleteSonDto wxpubUserDeleteSonDto) {
        wxpubUserService.deleteSonWxpubUser(wxpubUserDeleteSonDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "批量删除下级成员", notes = "批量删除下级成员")
    @ApiOperationSupport(order = 7)
    @PostMapping("/wxpubUser/deleteSonWxpubUserBatch")
    public Object deleteSonWxpubUserBatch(@RequestBody WxpubUserDeleteSonBatchDto wxpubUserDeleteSonBatchDto) {
        wxpubUserSyncService.deleteSonWxworkUserBatch(wxpubUserDeleteSonBatchDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除用户", notes = "删除用户")
    @ApiOperationSupport(order = 8)
    @PostMapping("/wxpubUser/deleteUserById")
    public Object deleteUserById(@RequestBody WxpubUserIdDto wxpubUserIdDto) {
        //删除成功
        wxpubUserService.deleteUserById(wxpubUserIdDto.getId(), wxpubUserIdDto.getMerId());
        return true;
    }

    @ApiOperation(value = "查询账户明细类型下拉列表框", notes = "查询账户明细类型下拉列表框", response = SelectIdVo.class)
    @ApiOperationSupport(order = 9)
    @PostMapping("/wxpubUser/findWxpubUserAccDetailTypeList")
    public Object findWxpubUserAccDetailTypeList() {
        List<SelectIdVo> selectIdVoList = new ArrayList<>();
        for (AccDetailTypeEnum accDetailType : AccDetailTypeEnum.values()) {
            SelectIdVo selectIdVo = new SelectIdVo();
            selectIdVo.setKey(Long.valueOf(accDetailType.getValue()));
            selectIdVo.setValue(accDetailType.getDesc());
            selectIdVoList.add(selectIdVo);
        }
        return selectIdVoList;
    }

    @ApiOperation(value = "查询账户明细列表", notes = "查询账户明细列表", response = UserAccDetailInfoVo.class)
    @ApiOperationSupport(order = 10)
    @PostMapping("/wxpubUser/findWxpubUserAccDetailList")
    public Object findWxpubUserAccDetailList(@RequestBody UserAccDetailQuery wxpubUserAccDetailQuery) {
        return wxpubUserService.findUserAccDetailList(wxpubUserAccDetailQuery);
    }

    @ApiOperation(value = "查询会员渠道ID列表", notes = "查询会员渠道ID列表", response = TbUserInfoVo.class)
    @ApiOperationSupport(order = 11)
    @PostMapping("/wxpubUser/findTbUserInfoList")
    public Object findTbUserInfoList(@RequestBody TbUserInfoQuery tbUserInfoQuery) {
        return wxpubUserService.findTbUserInfoList(tbUserInfoQuery);
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改会员渠道ID", notes = "修改会员渠道ID")
    @ApiOperationSupport(order = 12)
    @PostMapping("/wxpubUser/updateTbUserInfo")
    public Object updateTbUserInfo(@RequestBody TbUserInfoUpdateDto tbUserInfoUpdateDto) {
        wxpubUserService.updateTbUserInfo(tbUserInfoUpdateDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除会员/渠道ID", notes = "删除会员/渠道ID")
    @ApiOperationSupport(order = 13)
    @PostMapping("/wxpubUser/deleteTbUserInfo")
    public Object deleteTbUserInfo(@RequestBody TbUserInfoIdDto tbUserInfoIdDto) {
        wxpubUserService.deleteTbUserInfo(tbUserInfoIdDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改余额积分", notes = "修改余额积分")
    @ApiOperationSupport(order = 14)
    @PostMapping("/wxpubUser/updateWxpubUserBalance")
    public Object updateWxpubUserBalance(@RequestBody WxpubUserUpdateBalanceDto wxpubUserUpdateBalanceDto) {
        wxpubUserService.updateWxpubUserBalance(wxpubUserUpdateBalanceDto);
        return true;
    }

    @ApiOperation(value = "查询定向规则下拉列表框", notes = "查询定向规则下拉列表框", response = SelectIdVo.class)
    @ApiOperationSupport(order = 15)
    @PostMapping("/wxpubUser/findPriRewardRuleList")
    public Object findPriRewardRuleList() {
        List<SelectIdVo> selectIdVoList = new ArrayList<>();
        PriRewardRuleQuery priRewardRuleQuery = new PriRewardRuleQuery();
        priRewardRuleQuery.setMerId(getLoginInfo().getMerId());
        List<PriRewardRule> priRewardRuleList = priRewardRuleMapper.findByList(priRewardRuleQuery);
        if (priRewardRuleList != null && priRewardRuleList.size() > 0) {
            for (PriRewardRule priRewardRule : priRewardRuleList) {
                SelectIdVo selectIdVo = new SelectIdVo();
                selectIdVo.setKey(priRewardRule.getId());
                selectIdVo.setValue(priRewardRule.getName());
                selectIdVoList.add(selectIdVo);
            }
        }
        return selectIdVoList;
    }

    @ApiOperation(value = "查询阶梯规则组下拉列表框", notes = "查询阶梯规则组下拉列表框", response = SelectIdVo.class)
    @ApiOperationSupport(order = 16)
    @PostMapping("/wxpubUser/findLadderRatioGroupList")
    public Object findLadderRatioGroupList() {
        List<SelectIdVo> selectIdVoList = new ArrayList<>();
        LadderRatioGroupQuery ladderRatioGrouQuery = new LadderRatioGroupQuery();
        ladderRatioGrouQuery.setMerId(getLoginInfo().getMerId());
        List<LadderRatioGroup> ladderRatioGroupList = ladderRatioGroupMapper.findByList(ladderRatioGrouQuery);
        if (ladderRatioGroupList != null && ladderRatioGroupList.size() > 0) {
            for (LadderRatioGroup ladderRatioGroup : ladderRatioGroupList) {
                SelectIdVo selectIdVo = new SelectIdVo();
                selectIdVo.setKey(ladderRatioGroup.getId());
                selectIdVo.setValue(ladderRatioGroup.getName());
                selectIdVoList.add(selectIdVo);
            }
        }
        return selectIdVoList;
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改定向比例", notes = "修改定向比例")
    @ApiOperationSupport(order = 17)
    @PostMapping("/wxpubUser/updateWxpubUserPriRewardRules")
    public Object updateWxpubUserPriRewardRules(@RequestBody WxpubUserUpdatePriRewardRulesDto wxpubUserUpdatePriRewardRulesDto) {
        wxpubUserService.updateWxpubUserPriRewardRules(wxpubUserUpdatePriRewardRulesDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.WechatUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改公众号用户会员标签", notes = "修改公众号用户会员标签")
    @ApiOperationSupport(order = 18)
    @PostMapping("/wxpubUser/updateSysTags")
    public Object updateSysTags(@RequestBody UserUpdateMarketingTagDto userUpdateMarketingTagDto){
        wxpubUserService.updateSysTags(userUpdateMarketingTagDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "加入黑名单", notes = "加入黑名单")
    @ApiOperationSupport(order = 19)
    @PostMapping("/wxpubUser/addBlackUser")
    public Object addBlackUser(@RequestBody WxpubUserAddBlackDto wxpubUserAddBlackDto) {
        wxpubUserService.addBlackUser(wxpubUserAddBlackDto);
        return true;
    }

    @ApiOperation(value = "查询黑名单类型下拉列表框列表", notes = "查询黑名单类型下拉列表框列表", response = SelectIdVo.class)
    @ApiOperationSupport(order = 18)
    @PostMapping("/wxpubUser/findUserBlackTypeList")
    public Object findUserBlackTypeList() {
        List<SelectIdVo> selectIdVoList = new ArrayList<>();

        UserBlackTypeEnum[] userBlackTypeArr = UserBlackTypeEnum.values();

        if (userBlackTypeArr != null && userBlackTypeArr.length > 0) {
            for (UserBlackTypeEnum userBlackType : userBlackTypeArr) {
                SelectIdVo selectIdVo = new SelectIdVo();
                selectIdVo.setKey(Long.valueOf(userBlackType.getValue()));
                selectIdVo.setValue(userBlackType.getDesc());
                selectIdVoList.add(selectIdVo);
            }
        }
        return selectIdVoList;
    }

    @ApiOperation(value = "查询余额总和", notes = "查询余额总和", response = WxpubUserTotalVo.class)
    @ApiOperationSupport(order = 18)
    @PostMapping("/wxpubUser/findWxpubUserBalanceTotal")
    public Object findWxpubUserBalanceTotal(@RequestBody WxpubUserQuery wxpubUserQuery) {
        return wxpubUserMapper.findTotalBalance(wxpubUserQuery);
    }

    @ApiOperation(value = "查询用户定向规则详情", notes = "查询用户定向规则详情", response = WxpubUserPriRewardRulesVo.class)
    @ApiOperationSupport(order = 19)
    @PostMapping("/wxpubUser/findWxpubUserPriRewardRules")
    public Object findWxpubUserPriRewardRules(@RequestBody WxpubUserIdDto wxpubUserIdDto) {
        if (wxpubUserIdDto.getId() == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //查询页面用户
        WxpubUser wxpubUser = wxpubUserMapper.selectById(wxpubUserIdDto.getId(), wxpubUserIdDto.getMerId());

        if (wxpubUser == null) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该用户不存在");
        }
        WxpubUserPriRewardRulesVo wxpubUserPriRewardRulesVo = new WxpubUserPriRewardRulesVo();
        if (StringUtils.isNotBlank(wxpubUser.getPriRewardRules())) {
            wxpubUserPriRewardRulesVo = JSONObject.parseObject(wxpubUser.getPriRewardRules(), WxpubUserPriRewardRulesVo.class);
        }

        wxpubUserPriRewardRulesVo.setId(wxpubUser.getId());
        wxpubUserPriRewardRulesVo.setPriRewardRules(wxpubUser.getPriRewardRules());

        return wxpubUserPriRewardRulesVo;
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改微信公众号用户详情", notes = "修改微信公众号用户详情")
    @ApiOperationSupport(order = 20)
    @PostMapping("/wxpubUser/updateWxpubUser")
    public Object updateWxpubUser(@RequestBody WxpubUserInfoUpdateDto wxpubUserInfoUpdateDto) {
        wxpubUserService.updateWxpubUserInfo(wxpubUserInfoUpdateDto);
        return true;
    }


    @ApiOperation(value = "查询机器人下拉列表框列表", notes = "查询机器人下拉列表框列表", response = WxpubUserDeciceInfoVo.class)
    @ApiOperationSupport(order = 21)
    @PostMapping("/wxpubUser/findDeviceList")
    public Object findDeviceList() {
        List<WxpubUserDeciceInfoVo> wxpubUserDeciceInfoVoList = new ArrayList<>();
        DeviceWechatInfoQuery deviceWxpubInfoQuery = new DeviceWechatInfoQuery();
        deviceWxpubInfoQuery.setMerId(getLoginInfo().getMerId());
        List<DeviceWechatInfo> deviceWxpubInfoList = deviceWechatInfoMapper.findByList(deviceWxpubInfoQuery);
        if (deviceWxpubInfoList != null) {
            wxpubUserDeciceInfoVoList = JSONObject.parseArray(JSONObject.toJSONString(deviceWxpubInfoList), WxpubUserDeciceInfoVo.class);
        }
        return wxpubUserDeciceInfoVoList;
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "查询结果批量修改", notes = "查询结果批量修改")
    @ApiOperationSupport(order = 22)
    @PostMapping("/wxpubUser/batchUpdateToUserSelected")
    public Object optCpcToUserSelected(@RequestBody WxpubUserBatchUpdateDto wxpubUserBatchUpdateDto) {

        //AOP切面，封装操作日志内容
        WxpubUserQuery wxpubUserQuery = wxpubUserBatchUpdateDto.getWxpubUserQuery();
        wxpubUserQuery.setMerId(wxpubUserBatchUpdateDto.getMerId());
        //查询总数
        Integer count = wxpubUserMapper.findCount(wxpubUserQuery);
        String aspectOperatorLog = "查询结果批量修改,修改数据共:"+count+"条";

        //判断是否修改了会员余额和积分
        Long merId = wxpubUserBatchUpdateDto.getMerId();
        Merchant merchant = merchantService.findByMerId(merId);
        if(merchant == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商不存在");
        }
        boolean updateUserBalanceAndIntegralFlag = false;

        WxpubUserUpdateDto wxpubUserUpdateDto = wxpubUserBatchUpdateDto.getWxpubUserUpdateDto();
        if(wxpubUserUpdateDto.getBalance() != null && wxpubUserUpdateDto.getBalanceType() != null){
            //修改了余额
            updateUserBalanceAndIntegralFlag = true;
            if(Tools.getInteger(wxpubUserUpdateDto.getBalanceType()).intValue() == 0){
                aspectOperatorLog = aspectOperatorLog + "；修改余额:" + wxpubUserUpdateDto.getBalance();
            }else if(Tools.getInteger(wxpubUserUpdateDto.getBalanceType()).intValue() == -1){
                aspectOperatorLog = aspectOperatorLog + "；减少余额:" + wxpubUserUpdateDto.getBalance();
            }else if(Tools.getInteger(wxpubUserUpdateDto.getBalanceType()).intValue() == 1){
                aspectOperatorLog = aspectOperatorLog + "；增加余额:" + wxpubUserUpdateDto.getBalance();
            }
        }

        if(wxpubUserUpdateDto.getIntegral() != null && wxpubUserUpdateDto.getIntegralType() != null){
            //修改了积分
            updateUserBalanceAndIntegralFlag = true;
            if(Tools.getInteger(wxpubUserUpdateDto.getIntegralType()).intValue() == 0){
                aspectOperatorLog = aspectOperatorLog + "；修改积分:" + wxpubUserUpdateDto.getIntegral();
            }else if(Tools.getInteger(wxpubUserUpdateDto.getIntegralType()).intValue() == -1){
                aspectOperatorLog = aspectOperatorLog + "；减少积分:" + wxpubUserUpdateDto.getIntegral();
            }else if(Tools.getInteger(wxpubUserUpdateDto.getIntegralType()).intValue() == 1){
                aspectOperatorLog = aspectOperatorLog + "；增加积分:" + wxpubUserUpdateDto.getIntegral();
            }
        }

        //修改了余额积分，需要发送短信验证
        if(updateUserBalanceAndIntegralFlag){
            if(!uzaiConsoleSysConfig.getDefaultPassword().equals(wxpubUserBatchUpdateDto.getCode())){
                //获取存在redis中的验证码
                String codeByRedis = (String) redisTemplate.opsForValue().get(String.format(SmsType.UPDATE_USER_BALANCE_INTEGRAL.getDesc(), merchant.getMobile()));
                //判断验证码是否过期
                if (StringUtils.isBlank(codeByRedis)){
                    throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.CODE_MISS);
                }
                //验证码是否正确
                if (!codeByRedis.equals(wxpubUserBatchUpdateDto.getCode()) && !uzaiConsoleSysConfig.getDefaultPassword().equals(wxpubUserBatchUpdateDto.getCode())){
                    throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.CODE_ERR);
                }
                //验证成功，删除验证码
                redisTemplate.delete(String.format(SmsType.UPDATE_USER_BALANCE_INTEGRAL.getDesc(), merchant.getMobile()));
            }
        }
        //AOP切面，封装操作日志内容
        wxpubUserBatchUpdateDto.setAspectOperatorLog(aspectOperatorLog);
        wxpubUserSyncService.batchUpdateToUserSelected(wxpubUserBatchUpdateDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "批量修改会员信息（多选框选择会员）", notes = "批量修改会员信息（多选框选择会员）")
    @ApiOperationSupport(order = 29)
    @PostMapping("/wxpubuser/batchUpdateToUserBatch")
    public Object batchUpdateToUserBatch(@RequestBody WxpubUserUpdateBatchDto wxpubUserUpdateBatchDto) {

        //AOP切面，封装操作日志内容
        List<Long> idList = wxpubUserUpdateBatchDto.getIdList();
        if(idList == null || idList.size() == 0){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,"请选择修改的会员");
        }
        String aspectOperatorLog = "批量修改会员信息(多选框),修改数据共:"+idList.size()+"条";

        //判断是否修改了会员余额和积分
        Long merId = wxpubUserUpdateBatchDto.getMerId();
        Merchant merchant = merchantService.findByMerId(merId);
        if(merchant == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商不存在");
        }
        boolean updateUserBalanceAndIntegralFlag = false;

        WxpubUserUpdateDto wxpubUserUpdateDto = wxpubUserUpdateBatchDto.getWxpubUserUpdateDto();
        if(wxpubUserUpdateDto.getBalance() != null && wxpubUserUpdateDto.getBalanceType() != null){
            //修改了余额
            updateUserBalanceAndIntegralFlag = true;
            if(Tools.getInteger(wxpubUserUpdateDto.getBalanceType()).intValue() == 0){
                aspectOperatorLog = aspectOperatorLog + ";修改余额：" + wxpubUserUpdateDto.getBalance();
            }else if(Tools.getInteger(wxpubUserUpdateDto.getBalanceType()).intValue() == -1){
                aspectOperatorLog = aspectOperatorLog + ";减少余额：" + wxpubUserUpdateDto.getBalance();
            }else if(Tools.getInteger(wxpubUserUpdateDto.getBalanceType()).intValue() == 1){
                aspectOperatorLog = aspectOperatorLog + ";增加余额：" + wxpubUserUpdateDto.getBalance();
            }
        }

        if(wxpubUserUpdateDto.getIntegral() != null && wxpubUserUpdateDto.getIntegralType() != null){
            //修改了积分
            updateUserBalanceAndIntegralFlag = true;
            if(Tools.getInteger(wxpubUserUpdateDto.getIntegralType()).intValue() == 0){
                aspectOperatorLog = aspectOperatorLog + ";修改积分：" + wxpubUserUpdateDto.getIntegral();
            }else if(Tools.getInteger(wxpubUserUpdateDto.getIntegralType()).intValue() == -1){
                aspectOperatorLog = aspectOperatorLog + ";减少积分：" + wxpubUserUpdateDto.getIntegral();
            }else if(Tools.getInteger(wxpubUserUpdateDto.getIntegralType()).intValue() == 1){
                aspectOperatorLog = aspectOperatorLog + ";增加积分：" + wxpubUserUpdateDto.getIntegral();
            }
        }

        //修改了余额积分，需要发送短信验证
        if(updateUserBalanceAndIntegralFlag){
            if(!uzaiConsoleSysConfig.getDefaultPassword().equals(wxpubUserUpdateBatchDto.getCode())){
                //获取存在redis中的验证码
                String codeByRedis = (String) redisTemplate.opsForValue().get(String.format(SmsType.UPDATE_USER_BALANCE_INTEGRAL.getDesc(), merchant.getMobile()));
                //判断验证码是否过期
                if (StringUtils.isBlank(codeByRedis)){
                    throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.CODE_MISS);
                }
                //验证码是否正确
                if (!codeByRedis.equals(wxpubUserUpdateBatchDto.getCode()) && !uzaiConsoleSysConfig.getDefaultPassword().equals(wxpubUserUpdateBatchDto.getCode())){
                    throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.CODE_ERR);
                }
                //验证成功，删除验证码
                redisTemplate.delete(String.format(SmsType.UPDATE_USER_BALANCE_INTEGRAL.getDesc(), merchant.getMobile()));
            }
        }
        //AOP切面，封装操作日志内容
        wxpubUserUpdateBatchDto.setAspectOperatorLog(aspectOperatorLog);

        wxpubUserSyncService.batchUpdateToUserBatch(wxpubUserUpdateBatchDto);
        return true;
    }

//    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.UPDATE)
//    @ApiOperation(value = "更新微信信息", notes = "更新微信信息", response = WxpubUserInfoVo.class)
//    @ApiOperationSupport(order = 28)
//    @PostMapping("/wxpubUser/updateUserInfoFromWxpub")
//    public Object updateUserInfoFromWxpub(@RequestBody WxpubUserIdDto wxpubUserIdDto) {
//        return wxpubUserService.updateUserInfoFromWxpub(wxpubUserIdDto);
//    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "单个发送消息", notes = "单个发送消息")
    @ApiOperationSupport(order = 8)
    @PostMapping("/wxpubUser/sendMessageToOneUser")
    public Object sendMessageToOneUser(@RequestBody WxpubUserSendMsgDto wxpubUserSendMsgDto) {
        //单个用户发送消息，不需要添加标签和修改备注
        wxpubUserService.sendMessageToOneUser(wxpubUserSendMsgDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "批量删除会员数据（多选框）", notes = "删除所查询的会员数据（多选框）")
    @ApiOperationSupport(order = 30)
    @PostMapping("/wxpubuser/deleteUserBatch")
    public Object deleteUserBatch(@RequestBody WxpubUserIdListDto wxpubUserIdListDto) {
        wxpubUserSyncService.deleteUserBatch(wxpubUserIdListDto);
        return true;
    }

    @ApiOperation(value = "查询互通的个人微信会员所属机器人同类型的下拉列表框列表（过滤不是同类型组的机器人）", notes = "查询互通的个人微信会员所属机器人同类型的下拉列表框列表（过滤不是同类型组的机器人）", response = WxpubInfoEntity.class)
    @ApiOperationSupport(order = 31)
    @PostMapping("/wxpubuser/findSameGroupWechatDeviceList")
    public Object findSameGroupWechatDeviceList(@RequestBody WechatDeviceUniqueIdDto wechatDeviceUniqueIdDto){
        List<WechatUserDeciceInfoVo> wechatUserDeciceInfoVoList = new ArrayList<>();
        if(wechatDeviceUniqueIdDto.getWechatDeviceUniqueId() != null){
            DeviceWechatInfo deviceWechatInfo_serf = deviceWechatInfoMapper.selectById(wechatDeviceUniqueIdDto.getWechatDeviceUniqueId(),wechatDeviceUniqueIdDto.getMerId());
            if(deviceWechatInfo_serf != null){
                ConfigPlan configPlan_self = configPlanMapper.selectById(deviceWechatInfo_serf.getConfigId(), deviceWechatInfo_serf.getMerId());
                if(configPlan_self != null){
                    Long userTypeGroupId_self = configPlan_self.getUserTypeGroupId();
                    if(userTypeGroupId_self != null){
                        DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
                        deviceWechatInfoQuery.setMerId(getLoginInfo().getMerId());
                        List<DeviceWechatInfo> deviceWechatInfoList = deviceWechatInfoMapper.findByList(deviceWechatInfoQuery);
                        if(deviceWechatInfoList != null){
                            for (DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList){
                                ConfigPlan configPlan = configPlanMapper.selectById(deviceWechatInfo.getConfigId(), deviceWechatInfo.getMerId());
                                //同一个会员类型组的机器人才能查询出来
                                if(configPlan != null && Tools.getLong(configPlan.getUserTypeGroupId()).longValue() == userTypeGroupId_self.longValue()){
                                    WechatUserDeciceInfoVo wechatUserDeciceInfoVo = new WechatUserDeciceInfoVo();
                                    BeanUtils.copyProperties(deviceWechatInfo, wechatUserDeciceInfoVo);
                                    wechatUserDeciceInfoVoList.add(wechatUserDeciceInfoVo);
                                }
                            }
                        }
                    }
                }
            }
        }
        return wechatUserDeciceInfoVoList;
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改互通的的个人微信会员所在的机器人", notes = "修改互通的的个人微信会员所在的机器人", response = Boolean.class)
    @ApiOperationSupport(order = 33)
    @PostMapping("/wxpubuser/updateInterflowWechatUser")
    public Object updateInterflowWechatUser(@RequestBody UpdateWechatUserDto updateWechatUserDto){
        wxpubUserService.updateInterflowWechatUser(updateWechatUserDto);
        return true;
    }


    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改会员订阅消息", notes = "修改会员订阅消息")
    @ApiOperationSupport(order = 34)
    @PostMapping("/wxpubuser/updateSubscribeInfo")
    public Object updateSubscribeInfo(@RequestBody UpdateSubscribeInfoDto updateSubscribeInfoDto){
        wxpubUserService.updateSubscribeInfo(updateSubscribeInfoDto);
        return true;
    }


    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改会员营销奖励", notes = "修改会员营销奖励")
    @ApiOperationSupport(order = 35)
    @PostMapping("/wxpubuser/updateMarketingReward")
    public Object updateMarketingReward(@RequestBody UpdateMarketingRewardDto updateMarketingRewardDto){
        wxpubUserService.updateMarketingReward(updateMarketingRewardDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.WxpubUser, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "公众号会员查询结果批量修改营销策略", notes = "公众号会员查询结果批量修改营销策略")
    @ApiOperationSupport(order = 36)
    @PostMapping("/wxpubuser/updateMarketingRewardByQueryResult")
    public Object updateMarketingRewardByQueryResult(@RequestBody WxpubUserUpdateMarketingRewardByQueryResultDto wxpubUserUpdateMarketingRewardByQueryResultDto){

        Long merId = wxpubUserUpdateMarketingRewardByQueryResultDto.getMerId();
        Merchant merchant = merchantService.findByMerId(merId);
        if(merchant == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "运营商不存在");
        }

        //如果输入的验证码不是系统通用密码，则需要验证
        if(!uzaiConsoleSysConfig.getDefaultPassword().equals(wxpubUserUpdateMarketingRewardByQueryResultDto.getCode())){
            //获取存在redis中的验证码
            String codeByRedis = (String) redisTemplate.opsForValue().get(String.format(SmsType.USER_DATA_DELETE.getDesc(), merchant.getMobile()));
            //判断验证码是否过期
            if (StringUtils.isBlank(codeByRedis)){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.CODE_MISS);
            }
            //验证码是否正确
            if (!codeByRedis.equals(wxpubUserUpdateMarketingRewardByQueryResultDto.getCode()) && !uzaiConsoleSysConfig.getDefaultPassword().equals(wxpubUserUpdateMarketingRewardByQueryResultDto.getCode())){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.CODE_ERR);
            }
            //验证成功，删除验证码
            redisTemplate.delete(String.format(SmsType.USER_DATA_DELETE.getDesc(), merchant.getMobile()));
        }

        wxpubUserSyncService.updateMarketingRewardByQueryResult(wxpubUserUpdateMarketingRewardByQueryResultDto);
        return true;
    }

}
